package cn.itcast.utils;

import cn.itcast.bmap.distance.BMapDistanceVO;
import cn.itcast.bmap.geocoder.BMapGeocoderVO;
import cn.itcast.bmap.geocoder.Location;
import cn.itcast.constant.BMapConstant;
import cn.itcast.properties.BMapProperties;
import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Data
@Slf4j
@Component
@AllArgsConstructor
public class BMapUtil {

    private BMapProperties bMapProperties;

    public boolean isAddressOutofBound(String sourcePlace,String destPlace){
        //1、先拿到两个地址的经纬度信息
        Map<String, String> map = new HashMap<>();
        map.put("address",sourcePlace);
        map.put("output","json");
        map.put("ak",bMapProperties.getAk());
        BMapGeocoderVO sourceLocation = JSON.parseObject(HttpClientUtil.doGet(BMapConstant.GEOCODER_URL, map), BMapGeocoderVO.class);

        map = new HashMap<>();
        map.put("address",destPlace);
        map.put("output","json");
        map.put("ak",bMapProperties.getAk());
        BMapGeocoderVO destLocation = JSON.parseObject(HttpClientUtil.doGet(BMapConstant.GEOCODER_URL, map), BMapGeocoderVO.class);

        map=new HashMap<>();
        Location origins = sourceLocation.getResult().getLocation();
        Location destinations = destLocation.getResult().getLocation();
        map.put("origins",origins.getLat()+","+ origins.getLng());
        map.put("destinations",destinations.getLat()+","+ destinations.getLng());
        map.put("ak",bMapProperties.getAk());
        BMapDistanceVO bMapDistanceVO = JSON.parseObject(HttpClientUtil.doGet(BMapConstant.ROUTEMATRIX_URL, map), BMapDistanceVO.class);

        Long value = bMapDistanceVO.getResult().get(0).getDistance().getValue();

        return value > 5000;
    }
}
